package com.rjkj.medicalwaste.ui.setting

import android.os.Bundle
import android.util.Log
import android.view.*
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.core.view.MenuItemCompat
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.rjkj.medicalwaste.R
import com.rjkj.medicalwaste.adapter.ItemClick
import com.rjkj.medicalwaste.adapter.SearchHospitalAdapter
import com.rjkj.medicalwaste.base.BaseFragment
import com.rjkj.medicalwaste.bean.HospitalBean
import com.rjkj.medicalwaste.databinding.FragmentSearchHospitalBinding
import com.rjkj.medicalwaste.viewmodels.SearchHospitalViewModel
import com.rjkj.medicalwaste.viewmodels.UserViewModel
import com.rjkj.medicalwaste.viewmodels.provider.MainViewModelProvider


class SearchHospitalFragment : BaseFragment<FragmentSearchHospitalBinding>() {
    private val searchHospitalViewModel: SearchHospitalViewModel by viewModels()
    private val userViewModel: UserViewModel by activityViewModels {
        MainViewModelProvider.provideUserModel(requireContext())
    }

    private val searchHospitalAdapter =
        SearchHospitalAdapter(listOf(), object : ItemClick<HospitalBean> {
            override fun onClicked(view: View, value: HospitalBean) {
                Log.e(TAG, "onClicked: $value")
                userViewModel.bindHospital(
                    value.hospitalId,
                    userViewModel.getSerial(),
                    userViewModel.setting.value!!.cardNo
                )
            }
        })

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val binding = FragmentSearchHospitalBinding.inflate(inflater, container, false)
        onSubscribeUi(binding)
        binding.lifecycleOwner = viewLifecycleOwner
        return binding.root
    }

    override fun onSubscribeUi(binding: FragmentSearchHospitalBinding) {
        initTip(userViewModel)
        initTip(searchHospitalViewModel)
        binding.list.apply {
            val dividerItemDecoration =
                DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL)
            addItemDecoration(dividerItemDecoration)
            layoutManager = LinearLayoutManager(requireContext())
            adapter = searchHospitalAdapter
        }
        searchHospitalViewModel.hospitalList.observe(viewLifecycleOwner) {
            searchHospitalAdapter.list = it
            searchHospitalAdapter.notifyDataSetChanged()
        }
        binding.imBack.setOnClickListener {
            findNavController().navigateUp()
        }
        binding.search.onActionViewExpanded()
        binding.search.setIconifiedByDefault(true)
        binding.search.queryHint = "输入医院搜索"
        binding.search.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                if (query != null) {
                    searchHospitalViewModel.getQueryHospital(userViewModel.getUrl(),query)
                }
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                if (newText != null) {
                    searchHospitalViewModel.getQueryHospital(userViewModel.getUrl(),newText)
                }
                return false
            }

        })
    }
}